<!--
 * @Author: WuFeng <763467339@qq.com>
 * @Date: 2022-07-26 19:06:01
 * @LastEditTime: 2023-03-08 10:55:57
 * @LastEditors: WuFeng <763467339@qq.com>
 * @Description:
 * @FilePath: \vue3-element-plus-web\src\views\Demo\Print\index.vue
 * Copyright 版权声明
-->
<template>
  <div class="page-home app-container">

    <el-select v-model="formData.printerName" placeholder="请选择打印机">
      <el-option
        v-for="item in printNameList"
        :key="item"
        :label="item"
        :value="item"
      >
      </el-option>
    </el-select>
    <el-button type="primary" @click="handlePrint">
      打印
    </el-button>
    <el-button type="primary" @click="handleBatchPrint">
      批量打印
    </el-button>
  </div>
</template>

<script>
import { GSCloudPluginClass } from '@/plugins/GSCloudPlugin/GSCloudPluginClass.js'

export default {
  name: 'DemoPrint',
  data() {
    return {
      gsCloudPluginClass: null,
      gsCloudPlugin: null,
      printNameList: [],
      formData: {
        printerName: ''
      }
    }
  },
  watch: {
  },

  created() {
    const _this = this
    _this.gsCloudPluginClass = new GSCloudPluginClass()
    console.log(`🚀 ~ file: index.vue:37 ~ created ~ gsCloudPluginClass:`, _this.gsCloudPluginClass)
    // 加载插件js成功回调函数
    _this.gsCloudPluginClass.onLoadSuccess = function() {
      _this.gsCloudPlugin = _this.gsCloudPluginClass.Install
      // 获取系统打印机
      _this.gsCloudPluginClass.Install.GetPrinters({
        OnSuccess: function(result) {
          _this.printNameList = result.Data || []
        },
        OnError: function(result) {
        }
      })
    }
    // 加载插件js失败回调函数
    _this.gsCloudPluginClass.onLoadError = function() {
      console.log('光速云打印插件未安装或未启动')
    }
    // 加载插件js
    _this.gsCloudPluginClass.Load()
  },

  mounted() {
  },

  beforeUnmount() {
  },

  methods: {
    // 打印
    handlePrint() {
      this.gsCloudPlugin.PrintWord({
        Title: 'Word0001',
        // Width: 210,
        // Height: 297,
        Url: 'https://gsypp.com/files/demo/demo.docx',
        PrinterName: this.formData.printerName,
        ComposingMode: 'One', // 一张纸打印Word文档两页
        OnSuccess: function(result) {
          console.log('--print success', result)
        },
        OnError: function(result) {
          console.log('--print error', result)
        }
      })
    },
    // 批量打印
    handleBatchPrint() {
      const files = ['https://jkzc.hnlabour.com/bladex/upload/document/1627165392419176450/90d6d9708be051e8862ee17617b0bf62.docx',
        'https://jkzc.hnlabour.com/bladex/upload/document/1627165392419176450/ef0c703776cef8c7cf20b5cbcd32198a.docx']
      for (const item of files) {
        this.gsCloudPlugin.PrintWord({
          Title: 'Word0001',
          // Width: 210,
          // Height: 297,
          Url: item,
          PrinterName: this.formData.printerName,
          ComposingMode: 'One', // 一张纸打印Word文档两页
          OnSuccess: function(result) {
            console.log('--print success', result)
          },
          OnError: function(result) {
            console.log('--print error', result)
          }
        })
      }
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
</style>
